Import our data.
data("weather_df")
ggplot(data = weather_df, mapping = aes(x = tmin, y = tmax)) +
geom_point()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
weather_df %>%
ggplot(aes(x = tmin,y = tmax)) +
geom_point()
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
gg_weather_scatterplot =
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_point()
gg_weather_scatterplot
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
Check that some rows are missing.
weather_df %>%
filter(is.na(tmax))
## # A tibble: 17 × 6
## name id date prcp tmax tmin
## <chr> <chr> <date> <dbl> <dbl> <dbl>
## 1 Molokai_HI USW00022534 2022-05-31 NA NA NA
## 2 Waterhole_WA USS0023B17S 2021-03-09 NA NA NA
## 3 Waterhole_WA USS0023B17S 2021-12-07 51 NA NA
## 4 Waterhole_WA USS0023B17S 2021-12-31 0 NA NA
## 5 Waterhole_WA USS0023B17S 2022-02-03 0 NA NA
## 6 Waterhole_WA USS0023B17S 2022-08-09 NA NA NA
## 7 Waterhole_WA USS0023B17S 2022-08-10 NA NA NA
## 8 Waterhole_WA USS0023B17S 2022-08-11 NA NA NA
## 9 Waterhole_WA USS0023B17S 2022-08-12 NA NA NA
## 10 Waterhole_WA USS0023B17S 2022-08-13 NA NA NA
## 11 Waterhole_WA USS0023B17S 2022-08-14 NA NA NA
## 12 Waterhole_WA USS0023B17S 2022-08-15 NA NA NA
## 13 Waterhole_WA USS0023B17S 2022-08-16 NA NA NA
## 14 Waterhole_WA USS0023B17S 2022-08-17 NA NA NA
## 15 Waterhole_WA USS0023B17S 2022-08-18 NA NA NA
## 16 Waterhole_WA USS0023B17S 2022-08-19 NA NA NA
## 17 Waterhole_WA USS0023B17S 2022-12-31 76 NA NA
weather_df %>%
ggplot(aes(x=tmin, y=tmax, color = name)) +
geom_point(alpha = 0.3) + #makes points transparent to visualize density
geom_smooth(se = FALSE) #adds the trendlines, but removes the error lines
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
Where you define aesthetics can matter
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_point(aes(color = name), alpha = 0.3, size = 0.7) + #uniformly changes all sizes
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_point(aes(color = name, , size = prcp), alpha = 0.3) + #size of points dependent on precipitation intensity
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 19 rows containing missing values or values outside the scale range
## (`geom_point()`).
use faceting real quick
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_point(aes(color = name), alpha = 0.3, size = 0.8) +
geom_smooth(se = FALSE) +
facet_grid(. ~ name) #make multiple plots in a facet layout
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
# row ~ column - separate columns by name (nothing in row, column by name)
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_point(aes(color = name), alpha = 0.3, size = 0.8) +
geom_smooth(se = FALSE) +
facet_grid(name ~ .) # row ~ column - name by row, nothing in column
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
Let’s make a somewhat more interesting scatterplot.
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_point(aes(color = name, size = prcp), alpha = 0.3) +
geom_smooth(se = FALSE) +
facet_grid(. ~ name)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 19 rows containing missing values or values outside the scale range
## (`geom_point()`).
weather_df %>%
ggplot(aes(x=tmin, y=tmax, color = name, shape = name)) +
geom_point(aes(size = prcp), alpha = 0.3) +
geom_smooth(se = FALSE) +
facet_grid(. ~ name)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 19 rows containing missing values or values outside the scale range
## (`geom_point()`).
Learning assessment
weather_df %>%
filter(name == "CentralPark_NY") %>% #only want Central Park
mutate(
tmax_fhr = tmax * (9/5) + 32,
tmin_fhr = tmin * (9/5) + 32
) %>%
ggplot(aes(x = tmin_fhr, y = tmax_fhr, color = name)) + #doesn't separate by many colors because we have subset to just central park
geom_point() +
geom_smooth(se = FALSE, method = "lim") #makes linear regression line
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Computation failed in `stat_smooth()`.
## Caused by error in `get()`:
## ! object 'lim' of mode 'function' was not found
#no points
weather_df %>%
ggplot(aes(x=tmin, y=tmax, color = name, shape = name)) +
# geom_point(alpha = 0.3) can comment this out
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
#points painted on top of the smooth line - order matters
weather_df %>%
ggplot(aes(x=tmin, y=tmax, color = name, shape = name)) +
geom_smooth(se = FALSE) +
geom_point()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
We can change the spatial density too!
Use hexes instead of points. We can explicitly see the density change - communicate where data is centered.
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_hex()
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_binhex()`).
Change colors to be specific - not mapping the color, but one standard aesthetic. NOT within an aes, that will map the color.
weather_df %>%
ggplot(aes(x=tmin, y=tmax)) +
geom_point(color = "skyblue")
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_point()`).
Histogram.
weather_df %>%
ggplot(aes(x = tmin)) +
geom_histogram(color = "white", fill = "blue") #color is the outside border aesthetic, and the fill is the inside
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 17 rows containing non-finite outside the scale range
## (`stat_bin()`).